package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;

import java.net.URL;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;

public class PeopleBbsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		//http://bbs1.people.com.cn/post/2/1/2/124894818.html
		String[] regex = {
			"http://bbs[0-9]*.people.com.cn/post/[0-9]*/[0-9]*/[0-9]*/[0-9]*.html",
			"http://liuyan.people.com.cn/thread.php[?]display=&page=1&tid=[0-9]*"
		};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (!isDetailPage(urlMeta.getUrl())) {
		}
		NewsMeta bbs = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		bbs.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		Integer clickNum = 0;
		Integer commentNum = 0;

		Document doc = Jsoup.parse(htmltxt);

		doc.select("div.about-read").remove();
		doc.select("div.SogouKeywords").remove();
		doc.select("div#about").remove();
		doc.select("div#recommendTips").remove();

		title = null;
		Elements select = doc.select("h2");
		if(select!=null && select.size()!=0){
			title=select.get(0).text();
		}
		if(StringUtils.isBlank(title)){
			title = doc.select("h4").text();
			title = StringUtils.substringBefore(title, "|");
		}

		date = DateUtils.matchDate(doc.select("html body div.outer div.navBar div.articleInfo p.replayInfo span.float_l").text());
		if(date==null){
			date = DateUtils.matchDate(doc.select("span.time").text());
		}
		content = HtmlCleaner.getContentHtml(url,  doc.select("div.outer article"));
		if(StringUtils.isNotBlank(content)){
			if(content.indexOf("content_path=\"")!=-1){
				String contentUrl=content.substring(content.indexOf("content_path=\"")+"content_path=\"".length());
			    if(contentUrl.indexOf("\"")!=-1){
			    	contentUrl=contentUrl.substring(0,contentUrl.indexOf("\""));
			    }
			    try {
					Document docConent = Jsoup.parse(new URL(contentUrl), 10000);
					content = HtmlCleaner.getContentHtml(url,  docConent);
					if(content.startsWith(title)){
						content=content.substring(title.length());
					}
					docConent=null;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}else{
				Document parse = Jsoup.parse(content);
				content = HtmlCleaner.getContentHtml(url, parse.select("div"));
			}
		}
		if(StringUtils.isBlank(content)){
			content = doc.select("div.message").text();
		}

		String text = doc.select("html body div.outer div.navBar div.articleInfo p.replayInfo span.float_l span.readNum").text();

		try {
			clickNum = Integer.parseInt(text);
		} catch (NumberFormatException e) {
		}
		String text1 = doc.select("html body div.outer div.navBar div.articleInfo p.replayInfo span.float_l span.replayNum").text();
		try {
			commentNum = Integer.parseInt(text1);
		} catch (NumberFormatException e) {
		}
		bbs.setTitle(title);
		bbs.setContent(content);
		bbs.setDate(date);
		/**
		 * 解析用于更新的地址
		 */
		bbs.setUpdateUrl(url);

		bbs.setClickNum(clickNum);
		bbs.setCommentNum(commentNum);

		String author=doc.select("html body div.outer div.navBar div.articleInfo ul.author li div.clearfix a.userNick").text();
		bbs.setAuthor(author);
		return bbs;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if (meta != null) {
			String updateUrl = meta.getUpdateUrl();
			if (updateUrl != null) {
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
				if (responseToURL != null) {
					String html = responseToURL.getHtml();
					if (html != null) {
						Document doc = Jsoup.parse(html);
						String text = doc.select("html body dl.posts_c dd h2.clearfix strong b:eq(0)").text();
						String text1 = doc.select("html body dl.posts_c dd h2.clearfix strong b:eq(1)").text();

						try {
							int pos = text.indexOf(" ");
							if(pos!=-1){
								text =text.substring(0,pos);
							}
							meta.setClickNum(Integer.parseInt(text));
							meta.setCommentNum(Integer.parseInt(text1));
							return meta;
						} catch (NumberFormatException e) {
							// TODO Auto-generated catch block
						}
					}
				}
			}
		}
		return null;
	}

	public static void main(String[] args) {
		PeopleBbsAnalyse a = new PeopleBbsAnalyse();

		String url = "http://liuyan.people.com.cn/thread.php?tid=3112759&display=&page=1";
		System.out.println(DoMainUtils.GetDomainName(url));
		if (!a.isDetailPage(url)) {
			System.out.println("不匹配规则");
		} else {
			UrlMeta meta = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = a.parserHtml(meta);
			System.out.println(parserHtml);
//			System.out.println(a.Update(parserHtml));
		}

	}
	
}
